'''GeneralPreferencesPanel - prefs panel for application settings

The settings that this panel support are not unique to any particular
plugin, but rather to the app as a whole.
'''
import os
import sys
import wx
import rwb
import firebrick

SUPPORTED_FORMATS = ["tsv","html"]

class GeneralPreferencesPanel(firebrick.PreferencesPanel):
    location = ("General",)

    def __init__(self, parent, *args, **kwargs):
        super(GeneralPreferencesPanel, self).__init__(parent, *args, **kwargs)
#        rwb.PreferencesPanel.__init__(self, parent, *args, **kwargs)

        settings = rwb.app.GetSettings("rwb.general")

        format_label = wx.StaticText(self, wx.ID_ANY, "Preferred test suite format:")
        format_panel = wx.Panel(self, wx.ID_ANY)
        tsv_rb = wx.RadioButton(format_panel, wx.ID_ANY, "TSV", style=wx.RB_GROUP)
        html_rb = wx.RadioButton(format_panel, wx.ID_ANY, "HTML")
        tsv_rb.SetToolTipString("test suites are saved plain text, columns are separated by tabs")
        html_rb.SetToolTipString("test suites are saved as html documents")
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(tsv_rb)
        sizer.Add(html_rb)
        format_panel.SetSizer(sizer)
        tsv_rb.Bind(wx.EVT_RADIOBUTTON, self.OnFormatChanged)
        html_rb.Bind(wx.EVT_RADIOBUTTON, self.OnFormatChanged)
        
        # portDesc = wx.StaticText(self, wx.ID_ANY, 
        #                          "The port is used for communicating with a running robot test")
        # portLabel = wx.StaticText(self, wx.ID_ANY, "Port: ")
        # portEntry = wx.TextCtrl(self, wx.ID_ANY)

        sizer = wx.GridBagSizer(hgap=4, vgap=4)
        sizer.AddGrowableRow(3, 1)
        sizer.AddGrowableCol(3, 1)

#        sizer.Add(format_label, (1,1), flag=wx.RIGHT|wx.ALIGN_RIGHT, border=4)
        sizer.Add(format_label, (0,1), flag=wx.ALIGN_CENTER_VERTICAL)
        sizer.Add(format_panel, (0,2), flag=wx.ALIGN_CENTER_VERTICAL|wx.LEFT, border=8)
        self.SetSizer(sizer)

        default_format = settings.get("default_format", "tsv")
        if default_format.lower() == "html":
            html_rb.SetValue(True)
        else:
            tsv_rb.SetValue(True)

#        portEntry.SetValue(str(self.settings["port"]))
#        portEntry.Bind(wx.EVT_TEXT, self.OnPortChanged)

    def OnFormatChanged(self, event):
        '''Responds to the user clicking one of the format radiobuttons'''
        settings = rwb.app.GetSettings("rwb.general")
        settings["default_format"] = event.GetEventObject().GetLabel().lower()
        rwb.app.SaveSettings()

    def OnPortChanged(self, event):
        value = event.GetString()
        # if the value is a proper integer, persist it. If not,
        # don't change anything. This lets the user delete the whole
        # field without the software whining that the value isn't
        # a valid integer.
        try:
            port = int(value)
            self.settings["port"] = value
            rwb.app.SaveSettings()

        except ValueError:
            pass


